include ./config

TOPDIR = $(shell pwd)
SRC_DIR = $(TOPDIR)/Src
BIN_DIR = $(TOPDIR)/Bin
LIB_DIR = $(TOPDIR)/Lib

SYS_DIR = $(TOPDIR)/sys
THREAD_DIR = $(SYS_DIR)/thread
LOG_DIR = $(SYS_DIR)/log

export TOPDIR SYS_DIR LOG_DIR

SRC_SRCS = $(wildcard $(SRC_DIR)/*.cpp)
SRC_OBJS = $(patsubst %.cpp, %.o, $(SRC_SRCS))
THREAD_SRCS = $(wildcard $(THREAD_DIR)/*.cpp)
THREAD_OBJS = $(patsubst %.cpp, %.o, $(THREAD_SRCS))
LOG_SRCS = $(wildcard $(LOG_DIR)/*.cpp)
LOG_OBJS = $(patsubst %.cpp, %.o, $(LOG_SRCS))

LOG_LIB_NAME = $(LIB_DIR)/libLog.a
THREAD_LIB_NAME = $(LIB_DIR)/libThread.a
EXTLIBS = $(LIBDIR)/libLog.a

ifeq ($(BITS),64)
	LIB_SUFFIX = 64
endif

TARGET_NAME = $(BIN_DIR)/main$(LIB_SUFFIX)

all: $(TARGET_NAME)

output:
	@echo "BITS:"$(BITS)
	@echo $(LOG_OBJS)

$(TARGET_NAME): $(SRC_OBJS) $(LOG_LIB_NAME) $(THREAD_LIB_NAME)
	$(RM) $@;
	$(CPP) $^ $(CFLAGS) $(LDFLAGS) -o $@

$(LOG_LIB_NAME): $(LOG_OBJS)
	$(RM) $@;
	$(AR) $(AFLAGS) $@ $^

$(THREAD_LIB_NAME): $(THREAD_OBJS)
	$(RM) $@;
	$(AR) $(AFLAGS) $@ $^

.c.o:
	$(CC) -c $(CFLAGS) $^ -o $@

.cpp.o:
	$(CPP) -c $(CFLAGS) $^ -o $@

clean:
	$(RM) $(THREAD_OBJS) $(THREAD_TARGET_NAME) $(LOG_OBJS) $(SRC_OBJS) $(TARGET_NAME) \
	$(LOG_LIB_NAME) $(THREAD_LIB_NAME)
